<template>
  <CustomDialog :drag-dialog="dialogInfo" style="z-index: 1201">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      :disabled="!form.busPageInfo.editable"
      size="mini"
      label-width="160px"
    >
      <el-row :gutter="2">
        <el-col :span="8">
          <el-form-item label="组织机构" prop="busMainData.sysOrgCode">
            <OrgsetTreeSelect
              v-model="form.busMainData.sysOrgCode"
              :editable="
                form.busPageInfo.editable &&
                form.busMainData.funProcess === '10'
              "
              :has-priv="true"
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="业务时间" prop="busMainData.busDate">
            <el-date-picker
              v-model="form.busMainData.busDate"
              placeholder="业务时间"
              type="datetime"
              size="mini"
              clearable
              style="width: 200px"
              class="filter-item"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="提料入库仓库" prop="busMainData.warehouseId">
            <WarehouseTreeSelect
              v-model="form.busMainData.warehouseId"
              :editable="form.busPageInfo.editable"
              style="width: 300px"
              placeholder="选择仓库"
              @warehouseSelect="warehouseSelect"
              @setDefaultWarehouseId="setDefaultWarehouseId"
            />
          </el-form-item>
          <el-form-item label="备注" prop="busMainData.busRmk">
            <el-input
              v-model="form.busMainData.busRmk"
              size="mini"
              class="filter-item"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="发料机构" prop="busMainData.otherSysOrgCode">
            <OrgsetTreeSelect
              v-model="form.busMainData.otherSysOrgCode"
              :editable="
                form.busPageInfo.editable &&
                form.busMainData.funProcess === '10'
              "
              :has-priv="false"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <material-req-detail
      ref="materialReqDetail"
      :sup-form="thisForm"
      :material-req-details="form.materialReqDetails"
      :warehouse-id="form.busMainData.warehouseId"
      :editable="form.busPageInfo.editable"
      @setDetails="setDetails"
    />
    <div v-if="form.busPageInfo.editable" slot="footer" class="modal-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button
        v-permission="[
          'SYS_ADMIN',
          'COMMON_ADMIN',
          'OEM_MATERIAL_REQ_ALL',
          'OEM_MATERIAL_REQ_CHANGE',
          'OEM_MATERIAL_REQ_CREATE',
        ]"
        :loading="loading"
        type="primary"
        @click="doSave"
        >暂存</el-button
      >
      <el-button
        v-permission="[
          'SYS_ADMIN',
          'COMMON_ADMIN',
          'OEM_MATERIAL_REQ_ALL',
          'OEM_MATERIAL_REQ_SUBMIT',
        ]"
        :loading="loading"
        type="primary"
        @click="doSubmit"
        >提交</el-button
      >
    </div>
  </CustomDialog>
</template>

<script>
import oemMaterialReq from "@/api/prod/OemMaterialReq";
import MaterialReqDetail from "./oemMaterialReqDetail";
import OrgsetTreeSelect from "@/views/components/OrgsetTreeSelect";
import WarehouseTreeSelect from "@/views/base/warehouse/module/WarehouseTreeSelect";

export default {
  components: { MaterialReqDetail, OrgsetTreeSelect, WarehouseTreeSelect },
  props: {
    sup_this: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      isAdd: false,
      loading: false,
      thisForm: this,
      style: "width: 184px",
      dialogInfo: {
        name: "oemMaterialReqMngFormDialog",
        title: "提料单",
        fullScreen: true,
        width: 500,
        height: 90,
      },
      form: {
        materialReqDetails: [],
        busMainData: {
          id: "",
          enableFlag: "Y",
        },
        busPageInfo: {
          editable: false,
        },
      },
      rules: {
        "busMainData.busDate": [
          { required: true, message: "请选择业务时间", trigger: "blur" },
        ],
        "busMainData.sysOrgCode": [
          { required: true, message: "请选择机构", trigger: "blur" },
        ],
        "busMainData.otherSysOrgCode": [
          { required: true, message: "请选择发料机构", trigger: "blur" },
        ],
        "busMainData.warehouseId": [
          { required: true, message: "请选择仓库", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    cancel() {
      this.resetForm();
    },
    initDataOfBefore() {},
    displayDialog() {
      const { name } = this.dialogInfo;
      this.$nextTick(() => {
        this.$modal.show(name);
      });
    },
    hiddenDialog() {
      const { name } = this.dialogInfo;
      this.$nextTick(() => {
        this.$modal.hide(name);
      });
    },
    getOtherDetailData(mainId, replaceFlag, replaceDetailLotFlag) {
      if (mainId) {
        oemMaterialReq
          .getDetailData(mainId, replaceFlag, replaceDetailLotFlag)
          .then((res) => {
            this.form.materialReqDetails = res.data;
          });
      }
      /* setTimeout(() => {
        this.$refs.fileUpload.setFileList(this.form.attachmentList)
      }, 200) */
    },
    addData() {
      this.isAdd = true;
      const data = { paramBean: {} };
      oemMaterialReq
        .toAdd(data)
        .then((res) => {
          this.displayDialog();
          this.form = res.data;
          this.getOtherDetailData(null, true, true);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    copyData(mainId) {
      this.isAdd = true;
      const data = { paramBean: { id: mainId } };
      oemMaterialReq
        .toCopy(data)
        .then((res) => {
          this.displayDialog();
          this.form = res.data;
          this.getOtherDetailData(mainId, true, true);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    viewMainData(id) {
      this.isAdd = false;
      const data = { paramBean: { id: id } };
      oemMaterialReq.viewMainData(data).then((res) => {
        this.displayDialog();
        this.form = res.data;
        this.getOtherDetailData(id, false, false);
      });
    },
    doSubmit() {
      this.doSingleSave(true);
    },
    doSave() {
      this.doSingleSave(false);
    },
    doSingleSave(isSubmit) {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.formSubmit = isSubmit;
          this.loading = true;
          if (this.isAdd) {
            this.doAdd();
          } else {
            this.doEdit();
          }
        }
      });
    },
    doAdd() {
      oemMaterialReq
        .addOrEdit(this.form)
        .then((res) => {
          this.resetForm();
          this.loading = false;
          this.sup_this.init();
        })
        .catch((err) => {
          this.loading = false;
          console.log(err);
        });
    },
    doEdit() {
      oemMaterialReq
        .addOrEdit(this.form)
        .then((res) => {
          this.resetForm();
          this.loading = false;
          this.sup_this.init();
        })
        .catch((err) => {
          this.loading = false;
          console.log(err);
        });
    },
    resetForm() {
      this.hiddenDialog();
      this.$refs["form"].resetFields();
    },
    changeData(busId) {
      const data = { paramBean: { id: busId } };
      oemMaterialReq
        .toChange(data)
        .then((res) => {
          this.displayDialog();
          this.form = res.data;
          this.getOtherDetailData(this.form.busMainData.oldMainId, true, false);
        })
        .catch((err) => {
          this.dialog = false;
          console.log(err);
        });
    },
    setDetails(details) {
      this.form.materialReqDetails = details;
    },
    warehouseSelect(warehouseId) {
      this.form.materialReqDetails.forEach((tmpdetail) => {
        tmpdetail.storelocationId = null;
      });
    },
    setDefaultWarehouseId(warehouseId) {
      this.form.busMainData.warehouseId = warehouseId;
    },
  },
};
</script>
